package com.cjjd.c.utils;

import android.app.Activity;
import android.view.Gravity;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;

/**
 * ----------------------------------
 * Created by xyd on 2017/12/23.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class PopUtils {

    public static void dismissBottomPopWin(final PopupWindow popupWindow) {

        if (popupWindow==null){
            return;
        }
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);

        trans.setDuration(300);
        trans.setInterpolator(new AccelerateInterpolator());
        trans.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                popupWindow.dismiss();
            }
        });

        popupWindow.getContentView().startAnimation(trans);
    }


    /**
     * Show  popWindow
     *
     * @param activity
     * @param popupWindow
     */
    public static void showBottomPopWin(Activity activity, final PopupWindow popupWindow) {

        if (null != activity && popupWindow != null) {

            TranslateAnimation trans = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                    0, Animation.RELATIVE_TO_SELF, 1,
                    Animation.RELATIVE_TO_SELF, 0);

            if (popupWindow.isShowing()) {
                return;
            }
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM,
                    0, 0);
            trans.setDuration(300);
            trans.setInterpolator(new AccelerateDecelerateInterpolator());

            popupWindow.getContentView().startAnimation(trans);
        }
    }
}
